Disregard expired user_group rows in special page and API DB queries
[lhc/web/wiklou.git] / includes / specials / pagers / UsersPager.php
index 7b058c1..da3a30b 100644 (file)
@@ -100,7 +100,7 @@ class UsersPager extends AlphabeticPager {
         * @return array
         */
        function getQueryInfo() {
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_REPLICA );
                $conds = [];
 
                // Don't show hidden names
@@ -112,6 +112,7 @@ class UsersPager extends AlphabeticPager {
 
                if ( $this->requestedGroup != '' ) {
                        $conds['ug_group'] = $this->requestedGroup;
+                       $conds[] = 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() );
                }
 
                if ( $this->requestedUser != '' ) {
@@ -177,12 +178,12 @@ class UsersPager extends AlphabeticPager {
                $lang = $this->getLanguage();
 
                $groups = '';
-               $groups_list = self::getGroups( intval( $row->user_id ), $this->userGroupCache );
+               $ugms = self::getGroupMemberships( intval( $row->user_id ), $this->userGroupCache );
 
-               if ( !$this->including && count( $groups_list ) > 0 ) {
+               if ( !$this->including && count( $ugms ) > 0 ) {
                        $list = [];
-                       foreach ( $groups_list as $group ) {
-                               $list[] = self::buildGroupLink( $group, $userName );
+                       foreach ( $ugms as $ugm ) {
+                               $list[] = $this->buildGroupLink( $ugm, $userName );
                        }
                        $groups = $lang->commaList( $list );
                }
@@ -228,24 +229,32 @@ class UsersPager extends AlphabeticPager {
                }
 
                // Lookup groups for all the users
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = wfGetDB( DB_REPLICA );
                $groupRes = $dbr->select(
                        'user_groups',
-                       [ 'ug_user', 'ug_group' ],
+                       UserGroupMembership::selectFields(),
                        [ 'ug_user' => $userIds ],
                        __METHOD__
                );
                $cache = [];
                $groups = [];
                foreach ( $groupRes as $row ) {
-                       $cache[intval( $row->ug_user )][] = $row->ug_group;
-                       $groups[$row->ug_group] = true;
+                       $ugm = UserGroupMembership::newFromRow( $row );
+                       if ( !$ugm->isExpired() ) {
+                               $cache[$row->ug_user][$row->ug_group] = $ugm;
+                               $groups[$row->ug_group] = true;
+                       }
                }
+
+               // Give extensions a chance to add things like global user group data
+               // into the cache array to ensure proper output later on
+               Hooks::run( 'UsersPagerDoBatchLookups', [ $dbr, $userIds, &$cache, &$groups ] );
+
                $this->userGroupCache = $cache;
 
                // Add page of groups to link batch
                foreach ( $groups as $group => $unused ) {
-                       $groupPage = User::getGroupPage( $group );
+                       $groupPage = UserGroupMembership::getGroupPage( $group );
                        if ( $groupPage ) {
                                $batch->addObj( $groupPage );
                        }
@@ -335,7 +344,7 @@ class UsersPager extends AlphabeticPager {
        function getAllGroups() {
                $result = [];
                foreach ( User::getAllGroups() as $group ) {
-                       $result[$group] = User::getGroupName( $group );
+                       $result[$group] = UserGroupMembership::getGroupName( $group );
                }
                asort( $result );
 
@@ -360,36 +369,30 @@ class UsersPager extends AlphabeticPager {
        }
 
        /**
-        * Get a list of groups the specified user belongs to
+        * Get an associative array containing groups the specified user belongs to,
+        * and the relevant UserGroupMembership objects
         *
         * @param int $uid User id
         * @param array|null $cache
-        * @return array
+        * @return array (group name => UserGroupMembership object)
         */
-       protected static function getGroups( $uid, $cache = null ) {
+       protected static function getGroupMemberships( $uid, $cache = null ) {
                if ( $cache === null ) {
                        $user = User::newFromId( $uid );
-                       $effectiveGroups = $user->getEffectiveGroups();
+                       return $user->getGroupMemberships();
                } else {
-                       $effectiveGroups = isset( $cache[$uid] ) ? $cache[$uid] : [];
+                       return isset( $cache[$uid] ) ? $cache[$uid] : [];
                }
-               $groups = array_diff( $effectiveGroups, User::getImplicitGroups() );
-
-               return $groups;
        }
 
        /**
         * Format a link to a group description page
         *
-        * @param string $group Group name
+        * @param string|UserGroupMembership $group Group name or UserGroupMembership object
         * @param string $username Username
         * @return string
         */
-       protected static function buildGroupLink( $group, $username ) {
-               return User::makeGroupLinkHTML(
-                       $group,
-                       User::getGroupMember( $group, $username )
-               );
+       protected function buildGroupLink( $group, $username ) {
+               return UserGroupMembership::getLink( $group, $this->getContext(), 'html', $username );
        }
-
 }